package com.example.teacherservice.controller;

import com.example.teacherservice.dto.*;
import com.example.teacherservice.entity.Practice;
import com.example.teacherservice.service.CourseService;
import com.example.teacherservice.service.PracticeService;
import com.example.teacherservice.service.StudentService;
import com.example.teacherservice.service.TeacherService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/practices")
@RequiredArgsConstructor
public class PracticeController {
    private final PracticeService practiceService;
    private final TeacherService teacherService;
    private final CourseService courseService;
    private final StudentService studentService;

    // 获取教师的所有实践活动
    @GetMapping("/teacher/{teaId}")
    public ResponseEntity<List<PracticeDTO>> getPracticesByTeacher(@PathVariable Long teaId) {
        List<PracticeDTO> practices = practiceService.getPracticesByTeacherId(teaId);
        return ResponseEntity.ok(practices);
    }

    // 创建实践活动
    @PostMapping
    public ResponseEntity<?> createPractice(
            @RequestParam Long teaId,
            @Validated @RequestBody PracticeCreateDTO dto) {

        try {
            // 验证时间格式
            try {
                Timestamp.valueOf(dto.getStartTime());
                if(dto.getEndTime() != null) {
                    Timestamp.valueOf(dto.getEndTime());
                }
            } catch (IllegalArgumentException e) {
                return ResponseEntity.badRequest().body("时间格式不正确，应为 yyyy-MM-dd HH:mm:ss");
            }

            Practice practice = practiceService.createPractice(teaId, dto);
            return ResponseEntity.status(HttpStatus.CREATED).body(practice);

        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("创建失败: " + e.getMessage());
        }
    }

    // 获取实践活动详情
    @GetMapping("/{id}")
    public ResponseEntity<PracticeDTO> getPracticeDetail(@PathVariable Long id) {
        PracticeDTO practice = practiceService.getPracticeDetail(id);
        return ResponseEntity.ok(practice);
    }

    // 更新实践活动信息
    @PutMapping("/{id}")
    public ResponseEntity<Practice> updatePractice(
            @PathVariable Long id,
            @Validated @RequestBody PracticeUpdateDTO dto) {
        Practice practice = practiceService.updatePractice(id, dto);
        return ResponseEntity.ok(practice);
    }

    // 删除实践活动
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletePractice(@PathVariable Long id) {
        practiceService.deletePractice(id);
        return ResponseEntity.noContent().build();
    }


    // 关联学生到实践活动
    @PostMapping("/{id}/students")
    public ResponseEntity<Void> addStudentsToPractice(
            @PathVariable Long id,
            @RequestBody List<Long> studentIds) {
        practiceService.addStudentsToPractice(id, studentIds);
        return ResponseEntity.ok().build();
    }

    // 从实践活动移除学生
    @DeleteMapping("/{id}/students/{studentId}")
    public ResponseEntity<Void> removeStudentFromPractice(
            @PathVariable Long id,
            @PathVariable Long studentId) {
        practiceService.removeStudentFromPractice(id, studentId);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/{practiceId}/students/check")
    public ResponseEntity<Map<Long, Boolean>> checkStudentsInPractice(
            @PathVariable Long practiceId,
            @RequestParam List<Long> studentIds) {
        // 注意将 @RequestBody 改为 @RequestParam
        Map<Long, Boolean> result = practiceService.checkStudentsInPractice(practiceId, studentIds);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/{practiceId}/students")
    public ResponseEntity<List<StudentDTO>> getStudentsInPractice(
            @PathVariable Long practiceId) {
        List<StudentDTO> students = practiceService.getStudentsInPractice(practiceId);
        return ResponseEntity.ok(students);
    }


    @PatchMapping("/{id}/status")
    public ResponseEntity<Practice> updatePracticeStatus(
            @PathVariable Long id,
            @RequestBody Map<String, Integer> request) {

        Integer newStatus = request.get("status");
        Practice practice = practiceService.updatePracticeStatus(id, newStatus);
        return ResponseEntity.ok(practice);
    }


}